﻿/*Page created by Nivasan Pillay -- holds the information for the MyFoodTracker page*/

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace CSC440TeamOne.MyNutrition.Views
{
	public partial class MyFoodTracker : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMyFoodTrackerView
	{
		private MyFoodTrackerPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public MyFoodTrackerPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//	

        private void populate(Recipe recipe)
        {
            this.name.InnerText = recipe.name;
            this.repIngredients.DataSource = recipe.ingredients;
            this.repIngredients.DataBind();
        }

        protected void repIngredients_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, RecipeIngredient>.Bind(e.Item, a =>
            {
                a.Find<Label>("Label1").Text = a.BusinessItem.ToString();
            });
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            MyFoodTrackerViewModel mftvm = _presenter.getRecipesByValue(txtSearch.Text);
            this.repResults.DataSource = mftvm.searchResults;
            this.repResults.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.repAdd.DataBind();
        }

        /*protected void repResults_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            this.repResults.DataBind();
            List<Recipe> s = (List<Recipe>)repResults.DataSource;
            this.populate(s[e.Item.ItemIndex]);
        }*/

        protected void repResults_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyFoodTrackerViewModel mftvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mftvm.selectedRecipe);
        }

        protected void repResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("LinkButton1").Text = a.BusinessItem.name;
                a.Find<LinkButton>("LinkButton1").CommandArgument = a.BusinessItem.RecipeID.ToString();
            });
        }

        protected void repAdd_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyFoodTrackerViewModel mftvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mftvm.selectedRecipe);
        }

        protected void repAdd_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("LinkButton1").Text = a.BusinessItem.name;
            });
        }

        public void Show(MyFoodTrackerViewModel p)
        {
            this.repResults.DataSource = p.searchResults;
            this.repResults.DataBind();

            this.repAdd.DataSource = p.searchResults;
            this.repAdd.DataBind();
        }

        public void update(MyFoodTrackerViewModel model)
        {
            throw new NotImplementedException();
        }

	}
}

